// 表单验证公用方法
(function($){
	var defaults = {
		"control":[
			{"id":"","rule":"","errorText":""},{"id":"","rule":"","errorText":""}
		]
	}
	$.fn.vali= function(options){
		var opts = $.extend({},defaults,options);
		//规则正则对象
		var ruleList = {
			"num":/^[0-9]*$/,
			"userName":/^[a-zA-Z0-9_]{5,15}$/,
			"eMail":/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
			"password":/^[a-zA-Z0-9_]{5,15}$/,
			"notNull":"",
			"phoneNumber":/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
			"webSite":/^http:/
			};
		var submitBtn = $(":submit");
		//提交验证
		var tempFun = function(){
			$.each(opts.control,function(k,v){
				$that = $("input[name="+v.id+"],select[name="+v.id+"],textarea[name="+v.id+"]");
				$that.focus().blur();
			})
			if($(".falseText").size()>0){
				alert("有内容填写错误，请检查后填写完整进行提交。")
				return false;	
			}else{
				return true;
			}	
		}
		var trueIcon = "<i class='icon-ok-sign' style='font-size:14px'></i>",
			falseIcon = "<i class='icon-remove-sign' style='font-size:14px'></i>&nbsp;"
		
		//遍历绑定blur验证
		$.each(opts.control,function(k,v){
			$that = $("input[name="+v.id+"],select[name="+v.id+"],textarea[name="+v.id+"]");
			$that.on("blur",function(){
				$errorBox = $(this).nextAll('.helpBlock');
				//获取文本框label文本
				var errorTextTitle = $(this).prev().text().replace(":","");
				if(v.rule == "notNull"){
					//非空判断
					if($.trim($(this).val())==""||$(this).val()==null){
						if(v.errorText){
							$errorBox.html(falseIcon+v.errorText).addClass("falseText").removeClass("trueText");
						}else{
							$errorBox.html(falseIcon+"请输入"+errorTextTitle).addClass("falseText").removeClass("trueText");
						}
					}else if($(this).val()==0){
						$errorBox.html(falseIcon+"请选择"+errorTextTitle).addClass("falseText").removeClass("trueText");
					}else{
						$errorBox.html(trueIcon).removeClass("falseText").addClass("trueText");	
					}
					
				}
				
				var reg = ruleList[v.rule];
				if(reg){
					//正则判断
					!$(this).val().match(reg)?
						$errorBox.html(falseIcon+errorTextTitle+"输入错误!").addClass("falseText").removeClass("trueText")
						:
						$errorBox.html(trueIcon).removeClass("falseText").addClass("trueText");
					
					$(this).next().attr("class").indexOf("falseText")!=-1&&v.errorText&&v.errorText!=""?
						$errorBox.append("&nbsp;(注："+v.errorText+")")
						:
						$errorBox.append("");
				}
				//禁用提交
					
			});
		})
		submitBtn.on("click",function(){
			return tempFun();
		})
	}
	
})(jQuery)
